<?php
namespace app\pay\controller\common;

use think\Controller;
use app\pay\controller\yop\Order as YopOrder;
use app\pay\controller\yop\OrderQuery as YopOrderQuery;

use customtool\Utils;
/*
 * 下单中转接口
 */

class Order extends Base {
    /*
     * 下单 
     */
    public function index() {   
        $data = input('post.');
        $mod = array( // 必传参数
            0  => 'merchant_id',
            1  => 'mchnt_orderid',
            2  => 'pay_amount',
            3  => 'request_date',
            4  => 'pay_callbackurl',
            5  => 'pay_productname',
            6  => 'client_ip',
            7  => 'user_id',
            8  => 'user_type',
            9 => 'signature',
        );
        $resParams = Utils::checkParamsExists($data, $mod, null);
        
        if(!array_key_exists('timeout_express', $data)) {
            $data['timeout_express'] = 1440; // 分钟，24小时
        }
        if(!array_key_exists('pay_redirecturl', $data)) {
            $data['pay_redirecturl'] = null;
        }
        if(!array_key_exists('pay_productdesc', $data)) {
            $data['pay_productdesc'] = null;
        }
        if(!array_key_exists('timestamp', $data)) {
            $data['timestamp'] = time();
        }
        if(!array_key_exists('pay_tradetype', $data)) {
            $data['pay_tradetype'] = null;
        }
        if($resParams) {
            $result = $this->sendOrder($data);
            return $result;
        } else {
            return $resParams;
        }
    }
    
    private function sendOrder($data) {   
        $merchantId = $data['merchant_id'];
        // 根据商户号获取商户相关信息
        $resPayBis = model('PayBis')->get(['merchant_id' => $merchantId]);  
        if($resPayBis) {
            // 判断是否需要预充值手续费
            return $this->isPrepay($data, $resPayBis);
        } else {
            // 商户号不存在
            return paymchntIdFalse();
        }  
    }
    
    /*
     *  判断是否需要预充值手续费
     */
    private function isPrepay($data, $resPayBis) {
        // 支付通道费率采用：畅远收费＋平台收费＋固定每笔费用
        // 手续费，floor向下取整
        // 畅远与支付平台总收费
        // $tollFee = floor($data['pay_amount'] * ($resPayBis->mchnt_fee_rate + $resPayBis->pay_fee_rate) / 100);
        // 畅远收费
        $mchntFee = round($data['pay_amount'] * $resPayBis->mchnt_yjzf_rate / 100);
        if($resPayBis->is_prepay == 1 && $resPayBis->mchnt_pre_fee < $mchntFee) {
            // 支付通道使用费不足
            return payToollFeeFalse();
        } else {
            return $this->indexPlatform($data, $resPayBis);
        }
    }
    
    
    /*
     *  条件选择支付平台
     */
    private function indexPlatform($data, $resPayBis) {
        $resPayPlatform = model('PayPlatform')->get(['id' => $resPayBis->platform_id]);
        switch($resPayPlatform->platform) {
            case 'JuYin': // 聚银
                $result = 'JuYin';
            break;
            case 'FuMin': // 富民
                $result = 'FuMin';
            break;
            case 'Yop': // 易宝
                $YopOrder = new YopOrder();
                $result = $YopOrder->index($data, $resPayBis, $resPayPlatform);
            break;
        }
        return $result;
    }
     
    /*
     * ============================================我是最美的分割线==============================================================
     */   
    
    /*
     * 订单查询 
     */
    public function query() {
        $data = input('post.');
        if(array_key_exists('merchant_id', $data)) {
            $merchantId = $data['merchant_id'];
        } else {
            return retMsgFalse('缺少商户号');
        }
        // 根据商户号获取商户相关信息
        $resPayBis = model('PayBis')->get(['merchant_id' => $merchantId]);
        if($resPayBis) {
            if(array_key_exists('pay_orderid', $data)) {
                $payOrderId = $data['pay_orderid'];   // 平台订单号
            } else {
                $payOrderId = null;
            }

            if(array_key_exists('mchnt_orderid', $data)) {
                $mchntOrderId = $data['mchnt_orderid']; // 商户订单号  
            } else {
                $mchntOrderId = null;
            }

            if($payOrderId == null && $mchntOrderId == null) {
                return retMsgFalse('缺少订单号');
            }
            
            if($payOrderId != null) {
                $resPayBisOrder = model('PayBisOrder')->get(['pay_orderid' => $payOrderId]);
            } else {
                $resPayBisOrder = model('PayBisOrder')->get(['mchnt_orderid' => $mchntOrderId]);
            }
            
            if($resPayBisOrder) {
                $result = $this->queryPlatform($data, $resPayBis, $resPayBisOrder);
                return $result;
            } else {
                return orderidFalse(); // 订单号不存在
            }
        } else {
            // 商户号不存在
            return paymchntIdFalse();
        }
    }
    
    /*
     *  条件选择支付平台
     */
    private function queryPlatform($data, $resPayBis, $resPayBisOrder) {
        $resPayPlatform = model('PayPlatform')->get(['id' => $resPayBis->platform_id]);
        switch($resPayPlatform->platform) {
            case 'JuYin': // 聚银
                $result = 'JuYin';
            break;
            case 'FuMin': // 富民
                $result = 'FuMin';
            break;
            case 'Yop': // 易宝
                $YopOrderQuery = new YopOrderQuery();
                $result = $YopOrderQuery->index($data, $resPayBis, $resPayBisOrder, $resPayPlatform);
            break;
        }
        return $result;
    }
}

